/**
 * @BaseHelper.as
 * 
 * @author sodaChen mail:asframe@163.com
 * @version 1.0
 * <br>Copyright (C), 2010 asFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:TribeGameClient
 * <br>Date:2012-3-24
 */
package com.asframe.astruts2.mvc.controller
{
	import com.asframe.astruts2.mvc.mediator.MediatorAdapter;
	import com.asframe.astruts2.mvc.view.BaseUIView;
	
	import flash.events.Event;
	
	/**
	 *
	 * @author sodaChen
	 * @Date:2012-3-24
	 */
	public class BaseUIMediator extends MediatorAdapter
	{
		/** 是否马上初始化View **/
		protected var isSoonInitView:Boolean;
		
		public function BaseUIMediator(name:String=null)
		{
			super();
			isSoonInitView = true;
		}
		
		private function get baseUIView():BaseUIView
		{
			return view as BaseUIView;
		}
		public override function init():void
		{
			if(isSoonInitView)
			{
				checkInitView();
			}
			onInit();
		}
		public override function dispose(o:* = null):void
		{
			baseUIView.dispose();
			view = null;
		}
		/**
		 * 检测view有没有初始化。没有的话会自动初始化 
		 */		
		protected function checkInitView():void
		{
			if(!baseUIView.isInitView)
			{
				baseUIView.init();
				baseUIView.getContainer().addEventListener(BaseUIView.CLOSE_VIEW_EVENT,onViewCloseEvent,false,0,true);
			}
		}
		protected function onInit():void
		{
			
		}
		/**
		 * 注销事件的回调函数,主要是被子类进行重写
		 */		
		protected function onDispose():void
		{
		}
		/**
		 * view被销毁的回调函数，子类有事情做则需要重写该方法 
		 */		
		protected function onViewDispose():void
		{
			
		}
		
		public function onViewCloseEvent(event:Event):void
		{
			if(baseUIView.isCloseDispose)
			{
				onViewDispose();
				baseUIView.getContainer().removeEventListener(BaseUIView.CLOSE_VIEW_EVENT,onViewCloseEvent);
				baseUIView.dispose();
			}
		}
	}
}
